<template>
  <el-upload
    name="file"
    :show-file-list="false"
    accept="image/jpeg,image/png,image/gif,image/bmp,image/webp,image/tiff,image/svg+xml"
    :multiple="false"
    :http-request="uploadImage"
  >
    <div class="cover-upload-btn">
      <template v-if="modelValue">
        <img :src="proxy.globalInfo.imageUrl + modelValue" />
      </template>
      <span class="iconfont icon-add" v-else></span>
    </div>
  </el-upload>
</template>

<script setup>
import { getCurrentInstance } from "vue";

const api = {
  uploadUrl: "file/uploadImage",
};
const { proxy } = getCurrentInstance();
const emit = defineEmits();

const props = defineProps({
  modelValue: {
    type: String,
    default: null,
  },
});
const uploadImage = async (file) => {
  //   console.log(file.file);
  let result = await proxy.Request({
    url: api.uploadUrl,
    dataType: "file",
    params: {
      file: file.file,
      type: 0,
    },
  });
  const fileName = result.data.fileName;
  // console.log(proxy.globalInfo.imageUrl + fileName);
  emit("update:modelValue", fileName);
  //   console.log(result);
};
</script>

<style lang="scss" scoped>
.cover-upload-btn {
  width: 150px;
  height: 150px;
  background: #f7f7f7;
  display: flex;
  justify-content: center;
  align-items: center;
  .icon-add {
    font-size: 35px;
  }
  img {
    width: 100%;
  }
}
</style>
